import java.lang.Runnable;
import com.zxzr.SimpleTcpServer;
float x ;
float y ;
float z ;
int port = 54233;
String msg;
String[] values = {"0", "0", "0"};
private SimpleTcpServer mSTC = null;

void setup()
{
  size(1500, 750, P3D);
  rectMode(CENTER);
  openConnection();
}

void draw()
{
  background(255);  //clear screen.
  fill(0, 255, 0);
  translate(width/2, height/2); 
  rotateX(x);
  rotateY(y);
  rotateZ(z);
  box(400, 200, 100);
}

private void openConnection() {
  new Thread(new Runnable() {
    @Override
      public void run() {
      try {
        mSTC = new SimpleTcpServer(port);
        mSTC.accept();
        for (;; ) {
          msg = mSTC.recvMsg();
          if (null!=msg) {
            values =split(msg, ";");
            if (values.length ==3) {
              z = float(values[0]);
              x = float(values[1]);
              y = float(values[2]);
            }
          }
        }
      }
      catch (Exception e) {
        println(values.length);
        println(e);
      }
      finally {
        println("连接断开，请重试。");
        closeConnection();
      }
    }
  }        
  ).start();
}

private void closeConnection() {
  new Thread(new Runnable() {
    @Override
      public void run() {
      try {
        mSTC.closeEndpointSocket();
      } 
      catch (Exception e) {
        println(e.toString());
      } 
      finally {
        try {
          mSTC.closeServerSocket();
        } 
        catch (Exception e) {
          println(e.toString());
        }
      }
    }
  }
  ).start();
}
